//
//  VisiableView.swift
//  weibobbaj
//
//  Created by Tiffany宝 on 16/4/17.
//  Copyright © 2016年 Tiffany. All rights reserved.
//

import UIKit
import SnapKit

// 定义协议
// 需要继承于一个基协议 ： NSObjectProtocol
// 如果不继承，不能使用 weak 修饰
protocol VisiableViewDelegate:NSObjectProtocol{
    
    //func 不要漏
    func didClickRegisterButton()
    func didClickLoginButton()
}

class VisiableView: UIView {
    
    //***** 定义闭包 一直 犯的错误 *****
//    var registerClosure = (() -> ())?
    
    //正确方法
//    var registerClosure: (() -> ())?
    
    //遵守协议的代理 (用 weak 修饰)
    weak var delegate: VisiableViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(circleImgView)
        addSubview(coverImgView)
        addSubview(iconImgView)
        addSubview(textLabel)
        
        addSubview(registerButton)
        addSubview(loginButton)
        
        setupConstraint()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 抽取方法,在不同界面上显示不同的内容
    func setupVisibleInfo(imageNamed:String?,title:String?){
        
        //通过 imageNamed 和 title 是否有值,来判断此界面是否为 Home 界面
        if let imgName = imageNamed,titleText = title{
            
            iconImgView.image = UIImage(named: imgName)
            textLabel.text = titleText
            
        }else{
            
//            circleImgView.hidden = false
            setupAnim()
        }
    }
    
    private func setupAnim(){
        
        //显示圆圈
        circleImgView.hidden = false
        
        //动画转圈
        let iconAnimi = CABasicAnimation(keyPath: "transform.rotation")
        //设置旋转的弧度
        iconAnimi.toValue = 2 * M_PI
        //设置重复次数
        iconAnimi.repeatCount = MAXFLOAT
        //设置动画时长
        iconAnimi.duration = 20
        
        //**** 重要 ****
        // 如果界面切换，或者app退出到后台，默认会把动画给释放掉
        // 设置此属性，解决以上问题
        iconAnimi.removedOnCompletion = false
        
        //添加动画到 circleImgView
        circleImgView.layer.addAnimation(iconAnimi, forKey: nil)
    }
    
    private func setupConstraint(){
        
        //设置辉度(等价于---UIColor(colorLiteralRed: 237 / 255.0, green: 237 / 255.0, blue: 237 / 255.0, alpha: 1.0))
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1.0)
        
        circleImgView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        
        coverImgView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(circleImgView)
        }
        
        iconImgView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(coverImgView)
        }
        
        // **** snp_bottom ****
        textLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(circleImgView)
            make.top.equalTo(circleImgView.snp_bottom).offset(15)
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(textLabel)
            make.top.equalTo(textLabel.snp_bottom).offset(15)
            make.width.equalTo(100)
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.trailing.equalTo(textLabel)
            make.top.equalTo(registerButton)
            make.width.equalTo(100)
        }
    }
    
    
    // MARK: - 加载控件
    private lazy var iconImgView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var coverImgView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var circleImgView: UIImageView = {
        
        let imgView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        //默认隐藏 circleImgView
        imgView.hidden = true
        
        return imgView
        
    }()
    
    private lazy var textLabel: UILabel = {
        
        let label = UILabel(textFont: 14, textColor: UIColor.darkGrayColor())
        
        label.textAlignment = .Center
        
        label.text = "关注一些人，回这里看看有什么惊喜会发生"
        
        return label
    }()
    
    private lazy var registerButton: UIButton = {
        
        let button = UIButton(titleColor: UIColor.orangeColor(), titleFont: 14, imageName: "common_button_white")
        
        //添加按钮的监听事件
        button.addTarget(self, action: "didClickRegisterButton", forControlEvents: .TouchUpInside)
        
        button.setTitle("注册", forState: .Normal)
        
        return button
        
    }()
    
    private lazy var loginButton:UIButton = {
        
        let button = UIButton(titleColor: UIColor.darkGrayColor(), titleFont: 14, imageName: "common_button_white")
        
        //添加按钮的监听事件
        button.addTarget(self, action: "didClickLoginButton", forControlEvents: .TouchUpInside)
        
        button.setTitle("登录", forState: .Normal)
        
        return button
        
    }()
    
    @objc private func didClickRegisterButton(){
        
//        print("---didClickRegisterButton")
        
        //执行闭包
//        registerClosure?()
        
         delegate?.didClickRegisterButton()
    }
    
    @objc private func didClickLoginButton(){
        
//        print("---didClickLoginButton")
        
        //在 OC 中代理要先判断-->responseToSelector... , 在 Swift 中直接用 ? 搞定
         delegate?.didClickLoginButton()
    }
}

// MARK: - 用系统原生的约束方法
extension VisiableView{
    private func setupConstraint2(){
        
        //设置辉度(等价于---UIColor(colorLiteralRed: 237 / 255.0, green: 237 / 255.0, blue: 237 / 255.0, alpha: 1.0))
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1.0)
        
        //        //关闭 Autoresizing (因为与自动布局冲突)
        circleImgView.translatesAutoresizingMaskIntoConstraints = false
        //        /**
        //        参数1：要添加约束的对象
        //        参数2：要添加哪个约束
        //        参数3：约束与约束之前的关系 一般都用 equal
        //        参数4：参照约束的对象
        //        参数5：要约束参照对象的那个约束
        //        参数6：乘积
        //        参数7：偏移量
        //        */
        addConstraint(NSLayoutConstraint(item: circleImgView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleImgView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        coverImgView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: coverImgView, attribute: .CenterX, relatedBy: .Equal, toItem: circleImgView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverImgView, attribute: .CenterY, relatedBy: .Equal, toItem: circleImgView, attribute: .CenterY, multiplier: 1, constant: 0))
        
        iconImgView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconImgView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: coverImgView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconImgView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: coverImgView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: .Top, relatedBy: .Equal, toItem: circleImgView, attribute: .Bottom, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleImgView, attribute: .CenterX, multiplier: 1, constant: 0))
        
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Leading, relatedBy: .Equal, toItem: textLabel, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: textLabel, attribute: .Bottom, multiplier: 1, constant: 15))
        
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Trailing, relatedBy: .Equal, toItem: textLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: registerButton, attribute: .Top, multiplier: 1, constant: 0))
    }

}











